package mcom.xmks.bluetooth.mesh.storage;

import com.tencent.mmkv.MMKV;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import mcom.xmks.bluetooth.mesh.utils.LogTool;

public class GroupStorageUtil {
    private static final String MMKV_GROUP="mmkv_group";
    /**
     * 写入组
     * @param groupItem
     */
    public static void putGroup(GroupItem groupItem){
        MMKV defaultMMKV = MMKV.mmkvWithID(MMKV_GROUP);
        defaultMMKV.encode(createGroupAddress(groupItem.getAddress()),groupItem);
    }
    /**
     * 获取组
     * @param groupAddress
     * @return
     */
    public static GroupItem getGroup(int groupAddress){
        MMKV defaultMMKV = MMKV.mmkvWithID(MMKV_GROUP);
        return defaultMMKV.decodeParcelable(createGroupAddress(groupAddress), GroupItem.class);
    }
    /**
     * 删除组
     * @param groupAddress
     */
    public static void deleteGroup(int groupAddress){
        MMKV defaultMMKV = MMKV.mmkvWithID(MMKV_GROUP);
        defaultMMKV.remove(createGroupAddress(groupAddress));
    }
    /**
     * 添加节点到组
     * @param nodeAddress
     * @param groupAddress
     */
    public static void addNodeToGroup(int nodeAddress,int groupAddress){
        GroupItem group = getGroup(groupAddress);
        if(group!=null){
            List<String> nodeAddressItem = group.getNodeAddressItem();
            if(nodeAddressItem!=null) {
                String newItem = createGroupAddress(nodeAddress);
                if (!nodeAddressItem.contains(newItem)) {
                    nodeAddressItem.add(newItem);
                }
                updateGroupItems(groupAddress, nodeAddressItem);
            }
        }
    }
    /**
     * 从组中删除节点
     * @param nodeAddress
     * @param groupAddress
     */
    public static void removeNodeFromGroup(int nodeAddress,int groupAddress){
        GroupItem group = getGroup(groupAddress);
        if(group!=null){
            List<String> nodeAddressItem = group.getNodeAddressItem();
            if(nodeAddressItem!=null) {
                String newItem = createGroupAddress(nodeAddress);
                if (nodeAddressItem.contains(newItem)) {
                    nodeAddressItem.remove(newItem);
                    updateGroupItems(groupAddress, nodeAddressItem);
                }
            }
        }
    }
    /**
     * 从所有组中删除节点
     * @param nodeAddress 节点地址
     */
    public static void  removeNodeFromAllGroups(int nodeAddress){
        LogTool.d(String.format("remove node 0x${} from all group", nodeAddress));
        MMKV defaultMMKV = MMKV.mmkvWithID(MMKV_GROUP);
        String[] allKeys = defaultMMKV.allKeys();
        if(allKeys!=null) {
            for (String key : allKeys) {
                int gAddress = parseGroupAddress(key);
                removeNodeFromGroup(nodeAddress, gAddress);
            }
        }
    }
    /**
     * 更新组项目
     * @param groupAddress
     * @param nodeAddressItems
     */
    private  static void updateGroupItems(int groupAddress, List<String> nodeAddressItems){
        GroupItem group = getGroup(groupAddress);
        if(group!=null){
            putGroup(new GroupItem(group.getName(), group.getAddress(), nodeAddressItems));
        }
    }
    /**
     * 检查组是否存在
     * @param groupAddress
     * @return
     */
    public static boolean checkGroupHasExist(int groupAddress){
        List<GroupItem> allNode = getAllGroup();
        if(allNode!=null) {
            for (GroupItem groupItem : allNode) {
                if (groupItem.getAddress() == groupAddress) {
                    return true;
                }
            }
        }
        return false;
    }
    /**
     * 检查组中是否存在节点
     * @param nodeAddress
     * @param groupAddress
     * @return
     */
    public static boolean checkNodeHasExistInGroup(int nodeAddress,int groupAddress){
        GroupItem group = getGroup(groupAddress);
        if(group!=null){
            return group.getNodeAddressItem().contains(createGroupAddress(nodeAddress));
        }
        return false;
    }
    /**
     * 获取所有组
     * @return
     */
    public static List<GroupItem> getAllGroup(){
        MMKV defaultMMKV = MMKV.mmkvWithID(MMKV_GROUP);
        String[] allKeys = defaultMMKV.allKeys();
        List<GroupItem> mutableList=new ArrayList<>();
        if(allKeys!=null) {
            for (String key : allKeys) {
                GroupItem item = getGroup(parseGroupAddress(key));
                if (item != null) {
                    mutableList.add(item);
                }
            }
        }
        return mutableList;
    }
    /**
     * 获取组中的节点
     * @param groupAddress 组地址
     * @return
     */
    public static List<NodeItem> getNodesInGroup(int groupAddress){
        GroupItem group = getGroup(groupAddress);
        if(group!=null) {
             List<NodeItem> list =new ArrayList<>();
             List<String> nodeAddressItem=group.getNodeAddressItem();
            if(nodeAddressItem!=null) {
                for (String nodeItem : nodeAddressItem) {
                    NodeItem node = StorageUtil.getNode(nodeItem);
                    if (node != null) {
                        list.add(node);
                    }
                }
            }
            return list;
        }
        return null;
    }

    /**
     * 创建组地址
     * @param nodeAddress
     * @return
     */
    private static String createGroupAddress(int nodeAddress){
        return String.format(Locale.getDefault(),"%04X",nodeAddress);
    }
    /**
     * 解析组地址
     * @param nodeAddress
     * @return
     */
    private static int parseGroupAddress(String nodeAddress){
        return Integer.parseInt(nodeAddress,16);
    }
}
